home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DJGPP / CBGRX103.ZIP / contrib / libgrx / src / bitdraw.h < prev    next >
Text File  |  1993-12-06  |  12KB  |  380 lines

  1. /**
  2.  ** BITDRAW.H
  3.  **
  4.  **  Copyright (C) 1992, Csaba Biegl
  5.  **    820 Stirrup Dr, Nashville, TN, 37221
  6.  **    csaba@vuse.vanderbilt.edu
  7.  **
  8.  **  This file is distributed under the terms listed in the document
  9.  **  "copying.cb", available from the author at the address above.
  10.  **  A copy of "copying.cb" should accompany this file; if not, a copy
  11.  **  should be available from where this file was obtained.  This file
  12.  **  may not be distributed without a verbatim copy of "copying.cb".
  13.  **  You should also have received a copy of the GNU General Public
  14.  **  License along with this program (it is in the file "copying");
  15.  **  if not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  16.  **  Cambridge, MA 02139, USA.
  17.  **
  18.  **  This program is distributed in the hope that it will be useful,
  19.  **  but WITHOUT ANY WARRANTY; without even the implied warranty of
  20.  **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21.  **  GNU General Public License for more details.
  22.  **/
  23.  
  24. #ifndef _BITDRAW_H_
  25. #define _BITDRAW_H_
  26.  
  27. #ifdef  __TURBOC__
  28. #pragma inline
  29. #endif
  30.  
  31. /*
  32.  * utilities -- other files may define them too
  33.  */
  34. #ifndef _SaveDS
  35.  
  36. #ifdef  __TURBOC__
  37. #define _ClrDir()    asm cld
  38. #define _SetDir()    asm std
  39. #define _SaveDS()    asm push ds
  40. #define _RestoreDS()    asm pop  ds
  41. #endif
  42.  
  43. #ifdef  __GNUC__
  44. #define _ASV        asm volatile
  45. #define _ClrDir()    _ASV("cld")
  46. #define _SetDir()    _ASV("std")
  47. #define _SaveDS()
  48. #define _RestoreDS()
  49. #endif
  50.  
  51. #endif  /* _SaveDS */
  52.  
  53. /*
  54.  * instruction fragment to set up the EGA or VGA for subsequent
  55.  * bit mask register writes (__EGAINIT__) or just set up DX
  56.  * for the correct address (__EGAINITADDR__)
  57.  */
  58. #ifdef  __TURBOC__
  59. #define __EGAINIT__                                \
  60.     asm mov        dx,VGA_GR_CTRL_PORT;                    \
  61.     asm mov        al,VGA_BIT_MASK_REG;                    \
  62.     asm out        dx,al;                            \
  63.     asm inc        dx
  64. #define __EGAINITADDR__                                \
  65.     asm mov        dx,VGA_GR_CTRL_PORT+1;
  66. #endif
  67. #ifdef  __GNUC__
  68. #define __EGAINIT__                             "\n\
  69.     movl    $L_VGA_GR_CTRL_PORT,%%edx                  \n\
  70.     movb    $L_VGA_BIT_MASK_REG,%%al                  \n\
  71.     outb    %%al,%%dx                          \n\
  72.     incl    %%edx                                "
  73. #define __EGAINITADDR__                             "\n\
  74.     movl    $L_VGA_GR_CTRL_PORT+1,%%edx                    "
  75. #endif
  76.  
  77. /*
  78.  * mask and data output instruction fragments for line and font drawing
  79.  *   mask in al, data address in es:[di] (edi)
  80.  *   (ANDBYTE1: can destroy mask)
  81.  *   (ANDBYTE2: can use ah for temporary)
  82.  */
  83. #ifdef  __TURBOC__
  84. #define __INVMASK__        asm not        al
  85. #define __EGAMASK__        asm out        dx,al
  86. #define __EGABYTE__        asm inc        BYTE PTR es:[di]
  87. #define __VGABYTE__        asm and        BYTE PTR es:[di],al
  88. #define __XORBYTE__        asm xor        BYTE PTR es:[di],al
  89. #define __ORBYTE__        asm or        BYTE PTR es:[di],al
  90. #define __ANDBYTE1__                                \
  91.     asm not        al;                                \
  92.     asm and        BYTE PTR es:[di],al
  93. #define __ANDBYTE2__                                \
  94.     asm mov        ah,al;                            \
  95.     asm not        ah;                                \
  96.     asm and        BYTE PTR es:[di],ah
  97. #endif
  98. #ifdef  __GNUC__
  99. #define __INVMASK__        "notb   %%al"
  100. #define __EGAMASK__        "outb   %%al,%%dx"
  101. #define __EGABYTE__        "incb   (%%edi)"
  102. #define __VGABYTE__        "andb   %%al,(%%edi)"
  103. #define __XORBYTE__        "xorb   %%al,(%%edi)"
  104. #define __ORBYTE__        "orb    %%al,(%%edi)"
  105. #define __ANDBYTE1__                             "\n\
  106.     notb    %%al                              \n\
  107.     andb    %%al,(%%edi)                            "
  108. #define __ANDBYTE2__                             "\n\
  109.     movb    %%al,%%ah                          \n\
  110.     notb    %%ah                              \n\
  111.     andb    %%ah,(%%edi)                            "
  112. #endif
  113.  
  114. /*
  115.  * line drawing, X major
  116.  *   putmask: how to output the mask
  117.  *   putbyte: how to output the current byte
  118.  *   init:    what to do before starting
  119.  */
  120. #ifdef  __TURBOC__
  121. #define __XLINE__(addr,offs,deltx,delty,mask,putbyte,putmask,init) do {        \
  122.     init;                                    \
  123.     asm les        di,DWORD PTR addr;                        \
  124.     asm mov        si,WORD  PTR delty;                        \
  125.     asm mov        ax,WORD  PTR mask;                        \
  126.     asm mov        cx,WORD  PTR deltx;                        \
  127.     asm mov        bx,cx;                            \
  128.     asm shr        bx,1;                            \
  129.     asm inc        cx;                                \
  130.       Xline##putbyte##Loop:                            \
  131.     putmask;                                \
  132.     putbyte;                                \
  133.     asm ror        al,1;                            \
  134.     asm adc        di,0;                            \
  135.     asm sub        bx,si;                            \
  136.     asm jnc        Xline##putbyte##NoAdjust;                    \
  137.     asm add        bx,deltx;                            \
  138.     asm add        di,offs;                            \
  139.       Xline##putbyte##NoAdjust:                            \
  140.     asm loop    Xline##putbyte##Loop;                    \
  141. } while(0)
  142. #endif
  143. #ifdef  __GNUC__
  144. #define __XLINE__(adr,ofs,deltx,delty,msk,putbyte,putmask,init) _ASV(     "\n\
  145.     "init"                                  \n\
  146.     movl    %0,%%edi                          \n\
  147.     movl    %3,%%esi                          \n\
  148.     movl    %4,%%eax                          \n\
  149.     movl    %2,%%ecx                          \n\
  150.     movl    %%ecx,%%ebx                          \n\
  151.     shrl    $1,%%ebx                          \n\
  152.     incl    %%ecx                              \n\
  153. L_Xline"#putbyte"Loop:                              \n\
  154.     "putmask"                              \n\
  155.     "putbyte"                              \n\
  156.     rorb    $1,%%al                              \n\
  157.     adcl    $0,%%edi                          \n\
  158.     subl    %%esi,%%ebx                          \n\
  159.     jnc    L_Xline"#putbyte"NoAdjust                  \n\
  160.     addl    %2,%%ebx                          \n\
  161.     addl    %1,%%edi                          \n\
  162. L_Xline"#putbyte"NoAdjust:                          \n\
  163.     loop    L_Xline"#putbyte"Loop                       "\
  164.     : /* NOTHING */                                \
  165.     : "g" (adr), "g" (ofs), "g" (deltx), "g" (delty), "g" (msk)        \
  166.     : "di", "si", "dx", "cx", "bx", "ax"                    \
  167. )
  168. #endif
  169.  
  170. /*
  171.  * line drawing, Y major
  172.  *   putmask: how to output the mask
  173.  *   putbyte: how to output the current byte
  174.  *   init:    what to do before starting
  175.  */
  176. #ifdef  __TURBOC__
  177. #define __YLINE__(addr,offs,deltx,delty,mask,putbyte,putmask,init) do {        \
  178.     init;                                    \
  179.     asm mov        al,BYTE  PTR mask;                        \
  180.     asm les        di,DWORD PTR addr;                        \
  181.     asm mov        si,WORD  PTR offs;                        \
  182.     asm mov        cx,WORD  PTR delty;                        \
  183.     asm mov        bx,cx;                            \
  184.     asm shr        bx,1;                            \
  185.     asm inc        cx;                                \
  186.     putmask;                                \
  187.       Yline##putbyte##Loop:                            \
  188.     putbyte;                                \
  189.     asm sub        bx,deltx;                            \
  190.     asm jnc        Yline##putbyte##NoAdjust;                    \
  191.     asm add        bx,delty;                            \
  192.     asm ror        al,1;                            \
  193.     asm adc        di,0;                            \
  194.     putmask;                                \
  195.       Yline##putbyte##NoAdjust:                            \
  196.     asm add        di,si;                            \
  197.     asm loop    Yline##putbyte##Loop;                    \
  198. } while(0)
  199. #endif
  200. #ifdef  __GNUC__
  201. #define __YLINE__(adr,ofs,deltx,delty,msk,putbyte,putmask,init) _ASV(     "\n\
  202.     "init"                                  \n\
  203.     movl    %0,%%edi                          \n\
  204.     movl    %1,%%esi                          \n\
  205.     movl    %4,%%eax                          \n\
  206.     movl    %3,%%ecx                          \n\
  207.     movl    %%ecx,%%ebx                          \n\
  208.     shrl    $1,%%ebx                          \n\
  209.     incl    %%ecx                              \n\
  210.     "putmask"                              \n\
  211. L_Yline"#putbyte"Loop:                              \n\
  212.     "putbyte"                              \n\
  213.     subl    %2,%%ebx                          \n\
  214.     jnc    L_Yline"#putbyte"NoAdjust                  \n\
  215.     addl    %3,%%ebx                          \n\
  216.     rorb    $1,%%al                              \n\
  217.     adcl    $0,%%edi                          \n\
  218.     "putmask"                              \n\
  219. L_Yline"#putbyte"NoAdjust:                          \n\
  220.     addl    %%esi,%%edi                          \n\
  221.     loop    L_Yline"#putbyte"Loop                       "\
  222.     : /* NOTHING */                                \
  223.     : "g" (adr), "g" (ofs), "g" (deltx), "g" (delty), "g" (msk)        \
  224.     : "di", "si", "dx", "cx", "bx", "ax"                    \
  225. )
  226. #endif
  227.  
  228. #define _DrawLineEGA(AD,OF,X,Y,MS) do {                        \
  229.     if(X > Y) __XLINE__(AD,OF,X,Y,MS,__EGABYTE__,__EGAMASK__,__EGAINIT__);  \
  230.     else      __YLINE__(AD,OF,X,Y,MS,__EGABYTE__,__EGAMASK__,__EGAINIT__);  \
  231. } while(0)
  232.  
  233. #define _DrawLineVGA(AD,OF,X,Y,MS) do {                        \
  234.     if(X > Y) __XLINE__(AD,OF,X,Y,MS,__VGABYTE__,,);                \
  235.     else      __YLINE__(AD,OF,X,Y,MS,__VGABYTE__,,);                \
  236. } while(0)
  237.  
  238. #define _DrawLineXor(AD,OF,X,Y,MS) do {                        \
  239.     if(X > Y) __XLINE__(AD,OF,X,Y,MS,__XORBYTE__,,);                \
  240.     else      __YLINE__(AD,OF,X,Y,MS,__XORBYTE__,,);                \
  241. } while(0)
  242.  
  243. #define _DrawLineOr(AD,OF,X,Y,MS) do {                        \
  244.     if(X > Y) __XLINE__(AD,OF,X,Y,MS,__ORBYTE__,,);                \
  245.     else      __YLINE__(AD,OF,X,Y,MS,__ORBYTE__,,);                \
  246. } while(0)
  247.  
  248. #define _DrawLineAnd(AD,OF,X,Y,MS) do {                        \
  249.     if(X > Y) __XLINE__(AD,OF,X,Y,MS,__ANDBYTE2__,,);                \
  250.     else      __YLINE__(AD,OF,X,Y,MS,__ANDBYTE2__,,);                \
  251. } while(0)
  252.  
  253. /*
  254.  * font drawing
  255.  */
  256. #ifdef  __TURBOC__
  257. #define __FNTROW__(adr,bits,wdt,shft,mask,putbyte,putmask,init,inv) do {    \
  258.     init;                                    \
  259.     asm les        di,DWORD PTR adr;                        \
  260.     asm lds        si,DWORD PTR bits;                        \
  261.     asm mov        cx,WORD  PTR shft;                        \
  262.     asm jcxz    FontNS##putbyte##inv##Init;                    \
  263.     asm mov        bx,WORD  PTR wdt;                        \
  264.     asm or        bx,bx;                            \
  265.     asm jz        FontSFT##putbyte##inv##TestMask;                \
  266. FontSFT##putbyte##inv##Loop:                            \
  267.     asm lodsb;                                \
  268.     inv;                                    \
  269.     asm xor        ah,ah;                            \
  270.     asm ror        ax,cl;                            \
  271.     asm or        al,ch;                            \
  272.     asm mov        ch,ah;                            \
  273.     putmask;                                \
  274.     putbyte;                                \
  275.     asm inc        di;                                \
  276.     asm dec        bx;                                \
  277.     asm jnz        FontSFT##putbyte##inv##Loop;                \
  278. FontSFT##putbyte##inv##TestMask:                        \
  279.     asm or        bx,WORD  PTR mask;                        \
  280.     asm jz        Font##putbyte##inv##End;                    \
  281.     asm lodsb;                                \
  282.     inv;                                    \
  283.     asm shr        al,cl;                            \
  284.     asm or        al,ch;                            \
  285.     asm and        al,bl;                            \
  286.     asm jmp        Font##putbyte##inv##LastByte;                \
  287. FontNS##putbyte##inv##Init:                            \
  288.     asm mov        cx,WORD  PTR wdt;                        \
  289.     asm jcxz    FontNS##putbyte##inv##TestMask;                \
  290. FontNS##putbyte##inv##Loop:                            \
  291.     asm lodsb;                                \
  292.     inv;                                    \
  293.     putmask;                                \
  294.     putbyte;                                \
  295.     asm inc        di;                                \
  296.     asm loop    FontNS##putbyte##inv##Loop;                    \
  297. FontNS##putbyte##inv##TestMask:                            \
  298.     asm or        cx,WORD  PTR mask;                        \
  299.     asm jz        Font##putbyte##inv##End;                    \
  300.     asm lodsb;                                \
  301.     inv;                                    \
  302.     asm and        al,cl;                            \
  303. Font##putbyte##inv##LastByte:                            \
  304.     putmask;                                \
  305.     putbyte;                                \
  306. Font##putbyte##inv##End: ;                            \
  307. } while(0)
  308. #endif
  309. #ifdef  __GNUC__
  310. #define __FNTROW__(adr,bits,wdt,shft,mask,putbyte,putmask,init,inv) _ASV("\n\
  311.     "init"                                  \n\
  312.     movl    %0,%%edi                          \n\
  313.     movl    %1,%%esi                          \n\
  314.     movl    %3,%%ecx                          \n\
  315.     jecxz    L_FontNS"#putbyte #inv"Init                  \n\
  316.     movl    %2,%%ebx                          \n\
  317.     orl    %%ebx,%%ebx                          \n\
  318.     jz    L_FontSFT"#putbyte #inv"TestMask              \n\
  319. L_FontSFT"#putbyte #inv"Loop:                          \n\
  320.     lodsb                                  \n\
  321.     "inv"                                  \n\
  322.     xorb    %%ah,%%ah                          \n\
  323.     rorw    %%cl,%%ax                          \n\
  324.     orb    %%ch,%%al                          \n\
  325.     movb    %%ah,%%ch                          \n\
  326.     "putmask"                              \n\
  327.     "putbyte"                              \n\
  328.     incl    %%edi                              \n\
  329.     decl    %%ebx                              \n\
  330.     jnz    L_FontSFT"#putbyte #inv"Loop                  \n\
  331. L_FontSFT"#putbyte #inv"TestMask:                      \n\
  332.     orl    %4,%%ebx                          \n\
  333.     jz    L_Font"#putbyte #inv"End                  \n\
  334.     lodsb                                  \n\
  335.     "inv"                                  \n\
  336.     shrb    %%cl,%%al                          \n\
  337.     orb    %%ch,%%al                          \n\
  338.     andb    %%bl,%%al                          \n\
  339.     jmp    L_Font"#putbyte #inv"LastByte                  \n\
  340. L_FontNS"#putbyte #inv"Init:                          \n\
  341.     movl    %2,%%ecx                          \n\
  342.     jcxz    L_FontNS"#putbyte #inv"TestMask                  \n\
  343. L_FontNS"#putbyte #inv"Loop:                          \n\
  344.     lodsb                                  \n\
  345.     "inv"                                  \n\
  346.     "putmask"                              \n\
  347.     "putbyte"                              \n\
  348.     incl    %%edi                              \n\
  349.     loop    L_FontNS"#putbyte #inv"Loop                  \n\
  350. L_FontNS"#putbyte #inv"TestMask:                      \n\
  351.     orl    %4,%%ecx                          \n\
  352.     jz    L_Font"#putbyte #inv"End                  \n\
  353.     lodsb                                  \n\
  354.     "inv"                                  \n\
  355.     andb    %%cl,%%al                          \n\
  356. L_Font"#putbyte #inv"LastByte:                          \n\
  357.     "putmask"                              \n\
  358.     "putbyte"                              \n\
  359. L_Font"#putbyte #inv"End:                           "\
  360.     : /* NOTHING */                                \
  361.     : "g" (adr), "g" (bits), "g" (wdt), "g" (shft), "g" (mask)        \
  362.     : "di", "si", "dx", "cx", "bx", "ax"                    \
  363. )
  364. #endif
  365.  
  366. #define _CharFGRowEGA(A,B,W,S,M) __FNTROW__(A,B,W,S,M,__EGABYTE__,__EGAMASK__,__EGAINITADDR__,)
  367. #define _CharFGRowVGA(A,B,W,S,M) __FNTROW__(A,B,W,S,M,__VGABYTE__,,,)
  368. #define _CharFGRowXor(A,B,W,S,M) __FNTROW__(A,B,W,S,M,__XORBYTE__,,,)
  369. #define _CharFGRowOr(A,B,W,S,M)  __FNTROW__(A,B,W,S,M,__ORBYTE__,,,)
  370. #define _CharFGRowAnd(A,B,W,S,M) __FNTROW__(A,B,W,S,M,__ANDBYTE1__,,,)
  371.  
  372. #define _CharBGRowEGA(A,B,W,S,M) __FNTROW__(A,B,W,S,M,__EGABYTE__,__EGAMASK__,__EGAINITADDR__,__INVMASK__)
  373. #define _CharBGRowVGA(A,B,W,S,M) __FNTROW__(A,B,W,S,M,__VGABYTE__,,,__INVMASK__)
  374. #define _CharBGRowXor(A,B,W,S,M) __FNTROW__(A,B,W,S,M,__XORBYTE__,,,__INVMASK__)
  375. #define _CharBGRowOr(A,B,W,S,M)  __FNTROW__(A,B,W,S,M,__ORBYTE__,,,__INVMASK__)
  376. #define _CharBGRowAnd(A,B,W,S,M) __FNTROW__(A,B,W,S,M,__ANDBYTE1__,,,__INVMASK__)
  377.  
  378. #endif  /* whole file */
  379.  
  380.